<?php

namespace app\common\business;

use app\common\model\mysql\SysDictData as SysDictDataModel;

class SysDictData
{
    public $sysDictDataObj = null;
    public function __construct()
    {
        $this->sysDictDataObj = new SysDictDataModel();
    }

    //查询字典数据表
    public function getDictData($dictType){
        $dictData = $this->sysDictDataObj->getDictList($dictType);
        if(!$dictData){
            return [];
        }
        $dictData = $dictData->toArray();
        return $dictData;
    }

    /**
     * 查询字典数据列表
     * @param $where
     * @param $field
     * @param $limit
     * @param $page
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getNormalDictData($where,$field,$limit,$page){
        $dictData = $this->sysDictDataObj->getNormalDictDataList($where,$field,$limit,$page);
        if(!$dictData){
            return [];
        }
        $dictData = $dictData->toArray();
        return $dictData;
    }

    //添加字典类型数据
    public function addDictData($data){
        //根据$dict_type,$dict_label去数据库查询是否存在这条记录
        $postNameResult = $this->getDictDataByName($data['dict_type'],$data['dict_label']);
        if($postNameResult){
            throw new \think\Exception("该数据标签已经存在请重新设置");
        }
        //根据$dict_type,$dict_value去数据库查询是否存在这条记录
        $postCodeResult = $this->getDictByDataValue($data['dict_type'],$data['dict_value']);
        if($postCodeResult){
            throw new \think\Exception("该数据键值已经存在请重新设置");
        }
        try {
            $this->sysDictDataObj->save($data);
        }catch (\Exception $e){
            throw new \think\Exception($e->getMessage());
        }
        return $this->sysDictDataObj->id;
    }

    public function getDictDataByName($dict_type,$dict_label,$dict_code = ''){
        $user = $this->sysDictDataObj->getDictDataByNameModel($dict_type,$dict_label,$dict_code);
        if(!$user){
            return [];
        }
        return $user->toArray();
    }

    public function getDictByDataValue($dict_type,$dict_value,$dict_code = ''){
        $user = $this->sysDictDataObj->getDictDataByValueModel($dict_type,$dict_value,$dict_code);
        if(!$user){
            return [];
        }
        return $user->toArray();
    }

    //根据$id,去数据库查询是否存在这条记录
    public function getDictDataById($id){
        $user = $this->sysDictDataObj->getDictByDataId($id);
        if(!$user){
            return [];
        }
        return $user->toArray();
    }

    /**
     * 根据主键id更新数据表中的数据
     * @param $id
     * @param $data
     * @return bool
     */
    public function updateById($id,$data){
        $id = intval($id);
        if(empty($id) || empty($data) || !is_array($data)){
            return false;
        }
        //根据$dict_type,$dict_label去数据库查询是否存在这条记录
        $postNameResult = $this->getDictDataByName($data['dict_type'],$data['dict_label'],$id);
        if($postNameResult){
            throw new \think\Exception("该数据标签已经存在请重新设置");
        }
        //根据$dict_type,$dict_value去数据库查询是否存在这条记录
        $postCodeResult = $this->getDictByDataValue($data['dict_type'],$data['dict_value'],$id);
        if($postCodeResult){
            throw new \think\Exception("该数据键值已经存在请重新设置");
        }
        $where = [
            "dict_code"=>$id,
        ];
        return $this->sysDictDataObj->where($where)->find()->save($data);
    }

    //根据$ids数组，删除数据库记录delete
    public function DeldictDataIds($ids){
        $ids = explode(',', $ids);
        $result = $this->sysDictDataObj->DeleteDictDataIds($ids);
        return $result;
    }
}
